﻿// =================================================================== 
// 模块说明
//====================================================================
// 创建时间：2018-06-08 16:43:21
// 负责人：Sushee
// ===================================================================
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq.Expressions;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using OF.Utility;
using OF.Component.Model;
using OF.Component.IDAL;
using System.Text;
using OF.Component.DTO;

namespace OF.Component.DAL
{
	
	/// <summary>
	///消息 数据访问层
	/// </summary>	
	internal partial class Message
	{		
    
        /// <summary>
    	/// 根据断言条件获取单个消息
    	/// </summary>
        public MessageInfo QuerySingle(Expression<Func<MessageInfo, bool>> predicate)
	    {
            return SQLServerProvider.CreateObject(ConnectionStringKey, predicate);
        }
        /// <summary>
    	/// 根据断言条件获获取消息列表
    	/// </summary>
	    public IList<MessageInfo> QueryList(Expression<Func<MessageInfo, bool>> predicate)
	    {
	        return SQLServerProvider.CreateList(ConnectionStringKey, predicate);
	    }

        /// <summary>
        /// 获取店铺消息
        /// </summary>
        /// <param name="shopId"></param>
        /// <returns></returns>
        public List<OwnerMessageDto> SearchListPageByShopId(int shopId,int pageIndex,int pageSize,out int totalRecord)
        {
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetStoredProcCommand("up_Message_search");
            db.AddInParameter(dbc, "@ShopId", DbType.String, shopId);
            db.AddInParameter(dbc, "@PageIndex", DbType.Int32, pageIndex);
            db.AddInParameter(dbc, "@PageSize", DbType.Int32, pageSize);
            db.AddOutParameter(dbc, "@OutCount", DbType.Int32, 4);

            DataSet dr = db.ExecuteDataSet(dbc);
            totalRecord = (int)db.GetParameterValue(dbc, "@OutCount");

            return SQLServerProvider.CreateList<OwnerMessageDto>(dr.Tables[0]);
        }

	    public List<MessageInfo> SearchListFrontByShopId(int OwnerShopId, int pageIndex, int pageSize, out int totalRecord)
	    {
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetStoredProcCommand("up_MessageSearchFront");
            db.AddInParameter(dbc, "@OwnerShopId", DbType.String, OwnerShopId);
            db.AddInParameter(dbc, "@PageIndex", DbType.Int32, pageIndex);
            db.AddInParameter(dbc, "@PageSize", DbType.Int32, pageSize);
            db.AddOutParameter(dbc, "@OutCount", DbType.Int32, 4);

            DataSet dr = db.ExecuteDataSet(dbc);
            totalRecord = (int)db.GetParameterValue(dbc, "@OutCount");

            return SQLServerProvider.CreateList<MessageInfo>(dr.Tables[0]);
        }

	    public void SetMessageReaded(int shopId)
        {
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
         
            string szSql = @"  UPDATE Message
                                  SET ViewStatus = 2
                                  WHERE OwnerShopID = {0} AND ViewStatus = 1";
	        szSql = string.Format(szSql, shopId);
            DbCommand dbc = db.GetSqlStringCommand(szSql);
	        db.ExecuteNonQuery(dbc);
        }
	}
	
}